/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.views.platformer.movieclips
{
	import com.puny.mvc.helpers.FrameHelper;
	import com.puny.mvc.helpers.IHelper;
	
	import flash.display.FrameLabel;
	

	public class CharacterView extends ObjectView {
		public static const ANIM_END:String = "animation end";
		private var _frameEnd:Object = new Object();
		private var _labels:Array;
		private var _loopPrefix:Object = new Object();
		private var _helper:IHelper;
		private var _callback:Function;
		public function CharacterView() {
			super();
		}
		
		override public function beforeReady():void {
			super.beforeReady();
			var i:int;
			for (i = 0;i < totalFrames;i++) {
				super.gotoAndStop(i+1);
				_frameEnd[currentLabel] = currentFrame;
			}
			for each(var frame:FrameLabel in currentLabels) {
				for each(var label:String in _labels) {
					_loopPrefix[label] = (frame.name == LOOP_PREFIX + label) ? LOOP_PREFIX : (_loopPrefix[label] != null ? _loopPrefix[label] : "");
//					trace(label,_loopPrefix[label]);
				}
//				trace(frame.name,":",frame.frame,"-",_frameEnd[frame.name]);;
			}
			super.gotoAndStop(1);
		}

		public function enhancedPlay(frame:Object, callback:Function=null):void {
			var label:String;
			if (frame is String) {
				label = (loopPrefix[frame] == undefined ? "" : loopPrefix[frame])+frame;
//				trace((loopPrefix[frame] == undefined ? "" : loopPrefix[frame])+frame);
				if (label != currentLabel) {
					if (_helper != null) {
						_helper.skip();
					}
					gotoAndStop(label);
//					trace(currentLabel,label);
					_callback = callback;
					_helper = FrameHelper.setup(this,null,animation,afterAnimation);
				}
			} else {
				if (_helper != null) {
					_helper.skip();
				}
				gotoAndStop(frame);
				_callback = callback;
				_helper = FrameHelper.setup(this,null,animation, afterAnimation);
			}
		}
		
		private function afterAnimation():void {
			_helper = null;
			if (_callback != null) {
				_callback();
			}
		}
		
		protected function animation():Boolean {
//			trace(this,"---------------------",currentLabel, currentFrame);
			nextFrame();
//			trace(this,"---------------------",currentLabel, currentFrame);
			if (currentLabel != null && currentFrame == frameEnd[currentLabel]) {
				if (currentLabel.charAt(0) == LOOP_PREFIX) {
//					trace(this, "--------------------- because animation is looping");
					super.gotoAndStop(currentLabel);					
				} else {
//					trace(this, "---------------------", " stops here!",currentLabel,currentFrame);
					stop();
					return false;
				}
			}
			return true;
		}
		

		override public function gotoAndStop(frame:Object, scene:String=null):void {
			if (frame is String) {
//				trace(loopPrefix[frame],(loopPrefix[frame] == undefined ? "" : loopPrefix[frame])+frame);
				super.gotoAndStop((loopPrefix[frame] == undefined ? "" : loopPrefix[frame])+frame,scene);
			} else {
				super.gotoAndStop(frame,scene);
			}
		}
		
		public function get frameEnd():Object {
			return _frameEnd;
		}
		
		public function get labels():Array {
			return _labels;
		}
		
		public function set labels(value:Array):void {
			_labels = value;
		}
		
		public function get loopPrefix():Object {
			return _loopPrefix;
		}
		
		override public function beforeClose():void {
			super.beforeClose();
			if (_helper != null) {
				_helper.skip();
			}
		}
		
		public function pause():void {
			if (_helper != null) {
				_helper.pause();
			}
		}
		
		public function resume():void {
			if (_helper != null) {
				_helper.resume();
			}
		}
		
	}
}